package com.baofu.paycore.biz.engine.generator.impl;

import com.baofu.account.service.facade.enums.FreezeTypeEnum;
import com.baofu.paycore.biz.engine.command.Command;
import com.baofu.paycore.biz.engine.command.impl.PreAuthUnFreezeCommand;
import com.baofu.paycore.biz.engine.generator.CommandGenerator;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.manager.model.preAuth.PreAuthBO;
import com.baofu.paycore.manager.model.preAuth.SubPreAuthBO;
import com.baofu.paycore.service.facade.enums.PayToolTypeEnum;
import com.baofu.paycore.service.facade.enums.RequestSystemEnum;
import com.google.common.collect.Lists;
import com.system.commons.exception.BizServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 预授权撤销指令生成
 *
 * <p/>
 *      1、生成命令
 * </p>
 * User: xin deng  Date: 16/8/31 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Service
public class PreAuthCancelCommandGenerator implements CommandGenerator<PreAuthBO> {

    /**
     * 生成指令
     *
     * @param preAuthBO 订单模型
     * @return 命令列表
     */
    @Override
    public List<Command> generatorCommands(PreAuthBO preAuthBO) {
        List<Command> list = Lists.newArrayList();

        List<SubPreAuthBO> subPreAuthBOs = preAuthBO.getSubPreAuthBO();

        subPreAuthBOs.forEach(subPreAuthBO -> {
            PayToolTypeEnum payToolTypeEnum = PayToolTypeEnum.explain(subPreAuthBO.getPayToolType());
            switch (payToolTypeEnum){
                case ACCOUNT:
                    list.add(assemblePreAuthUnFreeze(preAuthBO, subPreAuthBO));
                    break;
                case BANK_CARD:
                    log.error("暂不支持银行卡预授权撤销功能:{}",preAuthBO.getPayNo());
                    throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT,"暂不支持银行卡预授权撤销功能");
            }
        });
        return list;
    }

    /**
     * 组装预授权解冻指令
     *
     * @param preAuthBO    预授权参数
     */
    private PreAuthUnFreezeCommand assemblePreAuthUnFreeze(PreAuthBO preAuthBO, SubPreAuthBO subPreAuthBO) {

        PreAuthUnFreezeCommand command = new PreAuthUnFreezeCommand();
        command.setRelationNo(preAuthBO.getRelationNo());
        command.setAmt(preAuthBO.getAmt());
        command.setBizType(preAuthBO.getBizType());
        command.setSubBizType(preAuthBO.getSubBizType());
        command.setCcy(preAuthBO.getCcy());
        command.setFundDetailNo(subPreAuthBO.getFundDetailNo());
        command.setOperator(preAuthBO.getOperator());
        command.setRequestDate(new Date());
        command.setRequestNo(subPreAuthBO.getFreezeNo());
        command.setRequestSystem(RequestSystemEnum.PAYCORE.getCode());
        command.setRequestNo(subPreAuthBO.getFundDetailNo());
        command.setAccountNo(subPreAuthBO.getPayerAccNo());
        command.setFreezeType(FreezeTypeEnum.DRAW.getCode());
        command.setFreezeCode(subPreAuthBO.getFreezeCode());
        command.setTradeLogId(preAuthBO.getTradeLogId());
        command.setServiceCode(preAuthBO.getServiceCode());
        command.setFundingChannel(subPreAuthBO.getFundingChannel());
        command.setSubFundingChannel(subPreAuthBO.getSubFundingChannel());
        command.setPayNo(preAuthBO.getPayNo());
        return command;
    }
}
